package com.question.view.command.play;

import java.util.Date;

import com.question.Design.SingletonMode;
import com.question.entity.Question;
import com.question.entity.User;
import com.question.service.IPlayerService;
import com.question.utils.InputOutUtils;
import com.question.view.Application;
import com.question.view.Configure;

public class AskCommand extends PlayerSuper {
	private IPlayerService playerService = SingletonMode.getPlayerService();
	@Override
	protected void exec(User player) {
		player = playerService.updatePlayer(player);
		Application.setUser(player);
		if(player.getScore() >= Configure.ASKREQUIRE) {
			InputOutUtils.print("请输入你的问题的标题按回车结束");
			String questionTitle = null;
			while(true) {
				questionTitle = InputOutUtils.inputString("", false);
				if(questionTitle.length() == 0 || questionTitle.length()>18) {
					InputOutUtils.print("请输入你的问题的标题应小于18个字符","是否再次输入");
					String string = InputOutUtils.inputString("[Y/N]", false);
					if("N".equalsIgnoreCase(string))return;
				}else {
					break;
				}
			}
			InputOutUtils.print("请输入你的问题按回车结束");
			String inVal = InputOutUtils.inputString("", false);
			InputOutUtils.print("请输入悬赏的"+Configure.INTEGRAL);
			Integer credit = InputOutUtils.inputInteger("");
			System.out.println(player.getScore());
			if(credit > player.getScore()) {
				InputOutUtils.print("你的"+Configure.INTEGRAL+"不够咯,先去回答问题赚取"+Configure.INTEGRAL);
				return;
			}
			Question question = new Question(null, inVal, new Date(System.currentTimeMillis()), credit, Question.OPEN_STATE_TYPE1,Question.QUESTION_STATE_TYPE1,player,questionTitle);
			playerService.askQuestion(question);
			InputOutUtils.print("问题发布成功");
			
		}else {
			InputOutUtils.print("你的"+Configure.INTEGRAL+"不足还不能提问,先回答问题吧");
		}
	}

}
